package top.infopub.mgr.la.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.la.dao.UpwardLawcaseAttDao;
import top.infopub.mgr.la.dao.UpwardLawcaseDao;
import top.infopub.mgr.la.dao.UpwardLawcaseProofAttDao;
import top.infopub.mgr.la.dao.UpwardLawcaseProofDao;
import top.infopub.mgr.la.domain.UpwardLawcaseDomain;
import top.infopub.mgr.la.service.UpwardLawcaseService;
import top.infopub.mgr.la.vo.UpwardLawcaseVo;
import top.infopub.model.la.UpwardLawcase;
import top.infopub.model.la.UpwardLawcaseAtt;
import top.infopub.model.la.UpwardLawcaseProof;
import top.infopub.model.la.UpwardLawcaseProofAtt;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class UpwardLawcaseServiceImpl extends GenericServiceImpl<UpwardLawcase, String> implements
		UpwardLawcaseService {

	@Autowired
	private UpwardLawcaseDao upwardLawcaseDao;
	
	@Autowired
	private UpwardLawcaseProofDao upwardLawcaseProofDao;
	
	@Autowired
	private UpwardLawcaseProofAttDao upwardLawcaseProofAttDao;
	
	@Autowired
	private UpwardLawcaseAttDao upwardLawcaseAttDao;
	
	@Autowired
	private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<UpwardLawcase, String> getDao() {
		return upwardLawcaseDao;
	}

    @Override
    public Page<UpwardLawcaseDomain> selectByDomainWhere(Page<UpwardLawcaseDomain> page,
                                                         UpwardLawcaseVo upwardLawcaseVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<UpwardLawcaseDomain>)upwardLawcaseDao.selectByDomainWhere(upwardLawcaseVo);
    }

    @Override
    public Page<UpwardLawcaseDomain> selectMyCheckByDomainWhere(Page<UpwardLawcaseDomain> page,
                                                                UpwardLawcaseVo upwardLawcaseVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<UpwardLawcaseDomain>)upwardLawcaseDao.selectMyCheckByDomainWhere(upwardLawcaseVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUpwardLawcase(UpwardLawcase upwardLawcase,
                                  List<UpwardLawcaseAtt> upwardLawcaseAtts,
                                  List<UpwardLawcaseProof> upwardLawcaseProofs,
                                  List<UpwardLawcaseProofAtt> upwardLawcaseProofAtts) {
        
        upwardLawcaseDao.insertById(upwardLawcase);
        
        if(CollectionUtils.isNotEmpty(upwardLawcaseAtts)){
            upwardLawcaseAttDao.insertByBatch(upwardLawcaseAtts);
        }
        
        if(CollectionUtils.isNotEmpty(upwardLawcaseProofs)){
            upwardLawcaseProofDao.insertByBatch(upwardLawcaseProofs);
        }
        
        if(CollectionUtils.isNotEmpty(upwardLawcaseProofAtts)){
            upwardLawcaseProofAttDao.insertByBatch(upwardLawcaseProofAtts);
        }
        
        // 1000万
        BigDecimal cmp = new BigDecimal("10000000");
        String srcBizTable = "LA_UPWARD_LAWCASE";
        String branchCode_lt = "target_amount<=1000万";
        String branchCode_gt = "target_amount>1000万";
        String templateId = "";
        // <=1000万
        if (upwardLawcase.getTargetAmount().compareTo(cmp) <= 0) {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_lt);
        }
        else {
            templateId = wsFlowProcess.queryTemplateIdByTableBranch(srcBizTable, branchCode_gt);
        }
        // 处理工作流数据
        wsFlowProcess.launch(templateId, srcBizTable, upwardLawcase.getId(),
            upwardLawcase.getProcessId(), null);
    }

    @Override
    public UpwardLawcaseVo queryById(String id) {
        return upwardLawcaseDao.queryById(id);
    }

    @Override
    public List<UpwardLawcase> selectUsablePeace() {
        return upwardLawcaseDao.selectUsablePeace();
    }

}